home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 14 / Mac Magazin and MacEasy Magazine CD - Issue 14.iso / Wissenschaft & Technik / Tools Plus 2.6.1 Evaluation Kit / Tools Plus 2.6.1 / Tutorials / 3-List Boxes / Tutorial.p < prev   
Text File  |  1995-08-25  |  7KB  |  218 lines

  1. {    Tools Plus Tutorial    - -    List Boxes    }
  2.  
  3. {    NOTE:    }
  4. {    The MWERKS compiler directived ($IFC MWERKS) indicates code that is compiled only by    }
  5. {    the CodeWarrior compiler.  THINK Pascal and Metrowerks CodeWarrior Pascal have    }
  6. {    _slight_ differences, and this compiler directive lets you use one source for both compilers.    }
  7. {    You can remove the code that does not pertain to your compiler.    }
  8.  
  9.  
  10. program Tutorial;
  11.     uses
  12. {$IFC MWERKS}
  13.         Dialogs, Fonts, Resources, Processes, SegLoad, TextEdit, ToolsPlus, Windows;
  14. {$ELSEC}
  15.         ToolsPlus;
  16. {$ENDC}
  17.  
  18.  
  19.     const
  20.     {Constants to make code more readable…}
  21.         LeftList = 1;
  22.         RightList = 2;
  23.  
  24.         MoveButton = 1;
  25.         RemoveButton = 2;
  26.         DoneButton = 255;
  27.  
  28.         LeftToRight = 1;
  29.         RightToLeft = -1;
  30.  
  31.     var
  32.         Poll: TPPollRecord;            {Polling record to retrieve event information}
  33.         ExitTheDemo: boolean;        {Should the demo terminate?}
  34.         Direction: integer;            {Direction in which lines are moved (left-to-right or right-to-left) }
  35.  
  36.  
  37.  
  38. {    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    }
  39.     procedure ApplicationInitialization;
  40.         const
  41.             DemoWindow1 = 1;
  42.         var
  43.             Font: FontInfo;            {Info about current font    }
  44.             BoxHeight: integer;        {List box's height in pixels    }
  45.  
  46.             resCount: integer;        {Number of resources    }
  47.             resIndex: integer;        {Resource index counter    }
  48.             hRsrc: handle;            {Handle to a resource    }
  49.             theResID: integer;        {Resource's ID    }
  50.             theResType: ResType;    {Resource's Type    }
  51.             theResName: Str255;        {Resource's Name    }
  52.             InsertLineAt: integer;        {Location where line should be inserted in list to be in alphabetic order    }
  53.     begin
  54.     {    Do all the application setup before you start polling for events…    }
  55.  
  56.         WindowOpen(DemoWindow1, 0, 0, 440, 130, '', dBoxProc + wCenter, NoGoAway, Modal);
  57.  
  58.     { Create left list box, making it exactly 6 lines high using Chicago 12pt…}
  59.         TextFont(systemFont);
  60.         TextSize(12);
  61.         GetFontInfo(Font);
  62.         BoxHeight := 6 * (Font.ascent + Font.descent + Font.leading);
  63.         NewListBox(LeftList, 15, 15, 145, 15 + BoxHeight, lOnlyOne);
  64.  
  65.     { Create left list box, making it exactly 8 lines high using Geneva 9pt…}
  66.         TextFont(geneva);
  67.         TextSize(9);
  68.         GetFontInfo(Font);
  69.         BoxHeight := 8 * (Font.ascent + Font.descent + Font.leading);
  70.         NewListBox(RightList, 280, 15, 410, 15 + BoxHeight, lOnlyOne);
  71.  
  72.     { Create the buttons used to control these lists…    }
  73.         NewButton(MoveButton, 175, 20, 265, 40, 'Move', pushButProc, disabled, notSelected);
  74.         NewButton(RemoveButton, 175, 50, 265, 70, 'Remove', pushButProc, disabled, notSelected);
  75.         NewButton(DoneButton, 185, 92, 255, 112, 'Done', pushButProc, enabled, notSelected);
  76.  
  77.  
  78.  
  79.     { Populate the left list with the names of all the fonts.  We are assuming that you are using System 6 or    }
  80.     { later, which uses 'FOND' resourses for fonts.}
  81.         DrawListBox(LeftList, false);    {Stop list box drawing for faster speed}
  82.         SetResLoad(false);                {Don't load resources for more speed}
  83.  
  84.         resCount := CountResources('FOND');    {How many fonts are there?}
  85.         for resIndex := 1 to resCount do
  86.             begin
  87.                 hRsrc := GetIndResource('FOND', resIndex);                {Get a handle to the font}
  88.                 GetResInfo(hRsrc, theResID, theResType, theResName);    {Get the font's ID, Type and Name    }
  89.  
  90.                 InsertLineAt := SearchListBox(LeftList, theResName);    {Where should this name go?}
  91.                 InsertListBoxLine(LeftList, InsertLineAt);                {Insert an empty line}
  92.                 SetListBoxText(LeftList, InsertLineAt, theResName);    {Put font name into empty line}
  93.             end;
  94.         SetResLoad(true);                {Resume loading resources}
  95.         DrawListBox(LeftList, true);    {Draw the list box's contents}
  96.  
  97.         Direction := none;
  98.         ExitTheDemo := false;
  99.     end;
  100.  
  101.  
  102. {    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    }
  103.     procedure ActionInWindow;
  104.         var
  105.             SourceList, TargetList: integer;    {Source and target list when moving lines}
  106.             SourceLine, TargetLine: integer;    {Line number in source and target list}
  107.             FontName: Str255;
  108.     begin
  109.         CurrentWindow(1);    {Subsequent actions affect window #1 (our only window)    }
  110.         case Poll.What of
  111.  
  112.             doListBox: 
  113.                 if GetListBoxLines(Poll.ListBox.Num, 1) = none then    {If an empty line was clicked…}
  114.                     begin    {Nothing selected, so disable the buttons and deselect lines in both lists…}
  115.                         Direction := none;
  116.                         ButtonTitle(MoveButton, 'Move');
  117.                         EnableButton(MoveButton, off);
  118.                         EnableButton(RemoveButton, off);
  119.                         SetListBoxLine(LeftList, max(1, GetListBoxLines(LeftList, 1)), off);
  120.                         SetListBoxLine(RightList, max(1, GetListBoxLines(RightList, 1)), off);
  121.                     end
  122.                 else
  123.                     begin    {If a non-empty line was clicked in the list box…}
  124.                         case Poll.ListBox.Num of
  125.                             LeftList: 
  126.                                 begin
  127.                                     Direction := LeftToRight;
  128.                                     ButtonTitle(MoveButton, '>> Move >>');
  129.                     { Turn off the line in the other list (if it has one selected)…    }
  130.                                     SetListBoxLine(RightList, max(1, GetListBoxLines(RightList, 1)), off);
  131.                                 end;
  132.  
  133.                             RightList: 
  134.                                 begin
  135.                                     Direction := RightToLeft;
  136.                                     ButtonTitle(MoveButton, '<< Move <<');
  137.                     { Turn off the line in the other list (if it has one selected)…    }
  138.                                     SetListBoxLine(LeftList, max(1, GetListBoxLines(LeftList, 1)), off);
  139.                                 end
  140.                         end;
  141.                         EnableButton(MoveButton, on);
  142.                         EnableButton(RemoveButton, on);
  143.                     end;
  144.  
  145.  
  146.             doButton: 
  147.                 case Poll.Button.Num of
  148.                     MoveButton:     {Move line from source list to target list…}
  149.                         begin
  150.                             if Direction = LeftToRight then
  151.                                 begin
  152.                                     SourceList := LeftList;
  153.                                     TargetList := RightList;
  154.                                     ButtonTitle(MoveButton, '<< Move <<');
  155.                                 end
  156.                             else
  157.                                 begin
  158.                                     SourceList := RightList;
  159.                                     TargetList := LeftList;
  160.                                     ButtonTitle(MoveButton, '>> Move >>');
  161.                                 end;
  162.                             SourceLine := GetListBoxLines(SourceList, 1);        {Find selected line in source list}
  163.                             GetListBoxText(SourceList, SourceLine, FontName);    {Get selected line's text}
  164.                             DeleteListBoxLine(SourceList, SourceLine);            {Delete line in source list}
  165.  
  166.                             TargetLine := SearchListBox(TargetList, FontName);    {Where should this name go in target list?}
  167.                             InsertListBoxLine(TargetList, TargetLine);            {Insert an empty line}
  168.                             SetListBoxText(TargetList, TargetLine, FontName);    {Put font name into empty line}
  169.                             SetListBoxLine(TargetList, TargetLine, on);            {Select line in target list}
  170.                             Direction := -Direction;                                {Change direction of movement}
  171.                         end;
  172.  
  173.                     RemoveButton:     {Remove the selected line…}
  174.                         begin
  175.                             if Direction = LeftToRight then
  176.                                 SourceList := LeftList
  177.                             else
  178.                                 SourceList := RightList;
  179.                             DeleteListBoxLine(SourceList, GetListBoxLines(SourceList, 1));
  180.                             Direction := none;
  181.                             ButtonTitle(MoveButton, 'Move');
  182.                             EnableButton(MoveButton, off);
  183.                             EnableButton(RemoveButton, off);
  184.                         end;
  185.  
  186.                     DoneButton:     {We're done, quit the application…}
  187.                         ExitTheDemo := true;
  188.                 end
  189.  
  190.         end
  191.     end;
  192.  
  193.  
  194. {    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    }
  195. begin
  196. {$IFC MWERKS}
  197. {Toolbox initialization - - Done automatically by THINK Pascal}
  198.     InitGraf(@qd.thePort);
  199.     InitFonts;
  200.     InitWindows;
  201.     InitMenus;
  202.     TEInit;
  203.     InitDialogs(nil);
  204.     MaxApplZone;
  205. {$ENDC}
  206.  
  207.     if not InitToolsPlus(0, 1, UseColor) then
  208.         ExitToShell;
  209.  
  210.     ApplicationInitialization;
  211.  
  212.     while not ExitTheDemo do    {Main Event Loop}
  213.         if PollSystem(Poll) then    {If an event is available, process the event…}
  214.             if (Poll.What = doListBox) or (Poll.What = doButton) then    {User clicked something in this window}
  215.                 ActionInWindow;
  216.  
  217.     {All other events are ignored}
  218. end.